package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.ApplicationForSupervisorDao;
import cn.edu.sdjzu.xg.bysj.domain.ApplicationForSupervisor;
import util.JdbcHelper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;


public class ApplicationForSupervisorService {
    private static ApplicationForSupervisorDao afsd = ApplicationForSupervisorDao.getInstance();
    private static ApplicationForSupervisorService afsf = new ApplicationForSupervisorService();

    public static ApplicationForSupervisorService getInstance(){
        return afsf;
    }

    public boolean add(List<ApplicationForSupervisor> collection_afs)throws SQLException {
        boolean added = false;
        Connection conn = JdbcHelper.getConn();

        conn.setAutoCommit(false);
        try {
            if (collection_afs.size()>0&&collection_afs.size()<=6){
                PreparedStatement pstmt1 =
                        conn.prepareStatement("DELETE FROM applicationforsupervisorentry");
                pstmt1.execute();
                PreparedStatement pstmt2 =
                        conn.prepareStatement("DELETE FROM applicationforsupervisor");
                pstmt2.execute();
                for (ApplicationForSupervisor afs:collection_afs){
                    added = afsd.add(afs,conn);
                }
            }else {
                return added;
            }
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return added;
    }
}
